<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.title"
          clearable
          placeholder="名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">类型</label>
        <el-select
          v-model="query.purchasetype"
          clearable
          filterable
          placeholder="请选择"
          style="width: 185px;"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.capital_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">购置金额</label>
        <el-input
          v-model="query.purchasenum"
          clearable
          placeholder="购置金额"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">资产所在地</label>
        <el-input
          v-model="query.location"
          clearable
          placeholder="资产所在地"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <br />
        <label class="el-form-item-label">创建时间</label>
        <el-date-picker
          v-model="query.gmtCreate"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="名称" prop="title">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="资产类型" prop="purchasetype">
            <el-select v-model="form.purchasetype" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.capital_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="购置金额">
            <el-input v-model="form.purchasenum" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="折损">
            <el-input v-model="form.lossrate" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="资产所在地">
            <el-input v-model="form.location" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="购置时间">
            <el-date-picker v-model="form.gmtCreate" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <!-- <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="title" label="名称" />
        <el-table-column prop="purchasetype" label="资产类型">
          <template slot-scope="scope">{{ dict.label.capital_type[scope.row.purchasetype] }}</template>
        </el-table-column>
        <el-table-column prop="purchasenum" label="购置金额" />
        <el-table-column prop="lossrate" label="折损" />
        <el-table-column prop="location" label="资产所在地" />
        <el-table-column prop="gmtCreate" label="购置时间" />
        <el-table-column
          v-if="checkPer(['admin','capital:edit','capital:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>-->
      <el-table
        :data="crud.data"
        style="width: 100%"
        ref="table"
        v-loading="crud.loading"
        size="small"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="名称">
                <span>{{ props.row.title }}</span>
              </el-form-item>
              <el-form-item label="类型">
                <span>{{ props.row.purchasetype }}</span>
              </el-form-item>
              <el-form-item label="购置金额">
                <span>{{ props.row.purchasenum }}</span>
              </el-form-item>
              <el-form-item label="折损">
                <span>{{ props.row.lossrate }}</span>
              </el-form-item>
              <el-form-item label="地址">
                <span>{{ props.row.location }}</span>
              </el-form-item>
              <el-form-item label="购置时间">
                <span>{{ props.row.gmtCreate }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="资产id" prop="id"></el-table-column>
        <el-table-column label="名称" prop="title"></el-table-column>
        <el-table-column label="购置时间" prop="gmtCreate"></el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCapital from "@/api/worth/worth";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";

const defaultForm = {
  id: null,
  username: null,
  title: null,
  purchasetype: null,
  purchasenum: null,
  lossrate: null,
  location: null,
  gmtCreate: null
};
export default {
  name: "Capital",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ["capital_type"],
  cruds() {
    return CRUD({
      title: "资产管理",
      url: "api/capital",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudCapital }
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "capital:add"],
        edit: ["admin", "capital:edit"],
        del: ["admin", "capital:del"]
      },
      rules: {
        title: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        purchasetype: [
          { required: true, message: "资产类型不能为空", trigger: "blur" }
        ]
      },
      queryTypeOptions: [
        { key: "title", display_name: "名称" },
        { key: "purchasetype", display_name: "资产类型" },
        { key: "purchasenum", display_name: "购置金额" },
        { key: "location", display_name: "资产所在地" }
      ]
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
    init() {
      this.query.username = window.sessionStorage.getItem("username");
      console.log("querry", this.query);
      this.crud.toQuery();
    }
  }
};
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
